package com.ruoyi.web.controller.dgw;

import java.io.IOException;
import java.util.List;

import com.ruoyi.common.FileOSSUtil;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.dgw.domain.TagNumber;
import com.ruoyi.dgw.service.ITagNumberService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.dgw.domain.Treatment;
import com.ruoyi.dgw.service.ITreatmentService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 无危化处理Controller
 *
 * @author ruoyi
 * @date 2021-11-30
 */
@RestController
@RequestMapping("/dgw/treatment")
@Slf4j
public class TreatmentController extends BaseController {
    @Autowired
    private ITreatmentService treatmentService;
    @Autowired
    private ITagNumberService tagNumberService;

    /**
     * 查询无危化处理列表
     */
    @PreAuthorize("@ss.hasPermi('dgw:treatment:list')")
    @GetMapping("/list")
    public TableDataInfo list(Treatment treatment) {
        startPage();
        List<Treatment> list = treatmentService.selectTreatmentList(treatment);
        return getDataTable(list);
    }

    /**
     * 导出无危化处理列表
     */
    @PreAuthorize("@ss.hasPermi('dgw:treatment:export')")
    @Log(title = "无危化处理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Treatment treatment) {
        List<Treatment> list = treatmentService.selectTreatmentList(treatment);
        ExcelUtil<Treatment> util = new ExcelUtil<Treatment>(Treatment.class);
        return util.exportExcel(list, "无危化处理数据");
    }

    /**
     * 获取无危化处理详细信息
     */
    @PreAuthorize("@ss.hasPermi('dgw:treatment:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(treatmentService.selectTreatmentById(id));
    }

    /**
     * 新增无危化处理
     */
    @PreAuthorize("@ss.hasPermi('dgw:treatment:add')")
    @Log(title = "无危化处理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(Treatment treatment) {
        return toAjax(treatmentService.insertTreatment(treatment));
    }

    /**
     * 修改无危化处理
     */
    @PreAuthorize("@ss.hasPermi('dgw:treatment:edit')")
    @Log(title = "无危化处理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Treatment treatment) {
        return toAjax(treatmentService.updateTreatment(treatment));
    }

    /**
     * 删除无危化处理
     */
    @PreAuthorize("@ss.hasPermi('dgw:treatment:remove')")
    @Log(title = "无危化处理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(treatmentService.deleteTreatmentByIds(ids));
    }

    @PostMapping("/importData")
    public AjaxResult uploadDocument(String earNumber,MultipartFile file)  {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        log.info("耳标编号接收：{}", earNumber);
        TagNumber tagNumber = tagNumberService.selectTagNumberByTagNumber(earNumber, sysUser.getUserId().toString());
        if (StringUtils.isNotNull(tagNumber)) {
            //if (StringUtils.isNotNull(file)) {
            Treatment treatment = new Treatment();
            treatment.setEarNumber(earNumber);
            treatment.setPhone(tagNumber.getPhone());
            treatment.setUserId(Long.valueOf(tagNumber.getUserId()));
            treatment.setAge(tagNumber.getIsUse());
            FileOSSUtil fileOSSUtil = new FileOSSUtil();
            String url = null;
            try {
                url = fileOSSUtil.uploadDocument(file);
            } catch (IOException e) {
                return AjaxResult.error("图片不能为空");
            }
            treatment.setApplicatUrl(url);
            treatment.setCreateBy(sysUser.getNickName());
            int i = treatmentService.insertTreatment(treatment);
            if (i > 0) {
                tagNumberService.updateEarNumber(earNumber);
            }
            return toAjax(i);
           /* }
            else {
                return AjaxResult.error("请添加图片");
            }*/
        } else {
            return AjaxResult.error("查询不到该耳标，请核对后重新输入");
        }
    }
}
